package com.jlboot.app.admin.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Record;
import com.jlboot.core.annotation.JlActivity;
import com.jlboot.core.base.BaseSearchDto;
import com.jlboot.core.exception.JlLogicException;
import com.jlboot.core.form.Form;
import com.jlboot.core.id.UUIDHex;
import com.jlboot.app.admin.ui.plugins.layui.LayuiData;
import com.jlboot.core.json.JsonResult;
import com.jlboot.app.admin.base.BaseController;
import com.jlboot.core.util.RequestUtil;
import com.jlboot.model.po.sys.SysApp;
import com.jfinal.plugin.activerecord.Page;
import jersey.repackaged.com.google.common.collect.Lists;

import java.util.List;

/**
 * Created by San-fei on 2023-02-24 10:22:34.
 */
public class SysAppController extends BaseController {

    @JlActivity( name = "SysApp首页", log = false)
    public void index() {
        renderText("SysApp首页");
    }

    /**
     * SysApp列表查询页面
     */
    @JlActivity( name = "SysApp列表查询页面", log = false)
    public void SysAppList() {
        render("SysAppList.html");
    }

    /**
     * SysApp新增页面
     */
    @JlActivity( name = "SysApp新增页面", log = false)
    public void AddSysApp() {
        Form fd = Form.n().setAction(Form.ADD);
        fd.addData("model",new SysApp().set("app_secret", UUIDHex.getUUIDHex()));

        setAttr("form", fd);
        render("ModifySysApp.html");
    }

    /**
     * SysApp修改页面
     */
    @JlActivity( name = "SysApp修改页面", log = false)
    public void EditSysApp() {
        String id = getPara("id");
        SysApp o = SysApp.dao.findById(id);
        Form fd = Form.n().setAction(Form.EDIT);
        fd.addData("model",o);

        setAttr("form", fd);
        render("ModifySysApp.html");
    }


    //-----------------API开始-------------------------------------//

    /**
     * SysApp列表查询API
     */
    @JlActivity( name = "SysApp列表查询API", log = false)
    public void SysAppListDataApi() {
        BaseSearchDto baseSearchDto=RequestUtil.parseToBaseSearchDto(getRequest());
        Page<Record> page = SysApp.dao.queryPage(baseSearchDto);
        renderJson(LayuiData.newData().setData(page.getList()).setCount(page.getTotalRow()));
    }

    /**
     * SysApp修改接口
     */
    @JlActivity( name = "SysApp修改接口", log = true)
    public void ModifySysAppApi() {

        JSONObject data = RequestUtil.parseJsonBody(getRequest());
        String action = data.getString("action");

        if (Form.EDIT.equals(action)) {
            SysApp.dao.update(data);
            renderJson(JsonResult.okWithMsg("修改成功"));
        }else if(Form.ADD.equals(action)){
            SysApp.dao.save(data);
            renderJson(JsonResult.okWithMsg("新增成功"));
        }else{
            throw new JlLogicException("action不能为空");
        }

    }


    /**
     * SysApp删除接口
     */
    @JlActivity( name = "SysApp删除接口", log = true)
    public void DelSysAppApi() {

        String id = getPara("id");
        SysApp.dao.delete(id);
        renderJson(JsonResult.okWithMsg("删除成功"));

    }


    /**
     * SysApp批量删除接口
     */
    @JlActivity( name = "SysApp批量删除接口", log = true)
    public void DelMoreSysAppApi() {

        String[] ids=getParaStr("keys", "").split(",");
        Integer result=SysApp.dao.deleteBatch(ids);
        renderJson(JsonResult.okWithMsg("删除成功，【共删除成功 " + result + ",失败 " + (ids.length-result) + "】"));

    }

    /**
     * SysApp修改部分字段接口
     */
    @JlActivity( name = "SysApp修改部分字段接口", log = true)
    public void ModifySysAppFieldApi() {

        String id = getPara("key_id");
        String field = getPara("field");
        String value = getPara("values");

        List<String> allowKeys= Lists.newArrayList("app_desc");
        if(!allowKeys.contains(field)){
            throw new JlLogicException("不允许修改该字段");
        }

        SysApp o = SysApp.dao.findById(id);
        if (o != null) {
            boolean status = o.set(field, value).update();
            if(status){
                renderJson(JsonResult.ok());
            }else{
                renderJson(JsonResult.fail());
            }
        } else {
            renderJson(JsonResult.okWithMsg("修改成功"));
        }
    }

    /**
     * SysApp重新生成app_secret接口
     */
    @JlActivity( name = "SysApp重新生成app_secret接口", log = false)
    public void InitAppSecretApi() {
        renderJson(JsonResult.ok(UUIDHex.getUUIDHex()));
    }

    /**
     * SysApp修改状态接口
     */
    @JlActivity( name = "SysApp修改状态接口", log = true)
    public void ChangeSysAppTagApi() {

        String key = getParaStr("key", "");
        String field = getParaStr("field", "");
        String msg = getParaStr("msg", "");
        renderJson(DoBasePub(SysApp.dao, key, field, msg));

    }
}